
package org.sopt.dev.ImgKnight.monitor.chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class PushedUrlMeter {

	private DefaultPieDataset pieDataSet = null;
	
	private TimeSeries totalPushed = null;
	
	private TimeSeries highPrioMemPush = null;
	
	private TimeSeries memPush = null;
	
	private TimeSeries cachePush = null;
	
	private TimeSeries diskPush = null;
	
	private TimeSeries pop = null;
	
	public PushedUrlMeter(int i) {
		pieDataSet = new DefaultPieDataset();
		pieDataSet.setValue("HighPrio", 0);
		pieDataSet.setValue("Memory", 0);
		pieDataSet.setValue("Cache", 0);
		pieDataSet.setValue("Disk", 0);
		pieDataSet.setValue("Pop", 0);
		totalPushed = new TimeSeries("Total");
		totalPushed.setMaximumItemAge(i);
		highPrioMemPush = new TimeSeries("HighPrio");
		highPrioMemPush.setMaximumItemAge(i);
		memPush = new TimeSeries("Memory");
		memPush.setMaximumItemAge(i);
		cachePush = new TimeSeries("Cache");
		cachePush.setMaximumItemAge(i);
		diskPush = new TimeSeries("Disk");
		diskPush.setMaximumItemAge(i);
		pop = new TimeSeries("Pop");
		pop.setMaximumItemAge(i);
	}
	
	public JFreeChart createPieChart() {
		JFreeChart jfreechart = ChartFactory.createPieChart("Total Pushed URL(1)", 
				pieDataSet, false, true, false);
		PiePlot plot = (PiePlot)jfreechart.getPlot();
		plot.setLabelFont(new Font("SansSerif", 1, 10));
		plot.setNoDataMessage("No data available");
		plot.setCircular(false);
        plot.setLabelGap(0.01);
        jfreechart.setBorderPaint(Color.white);
		jfreechart.setBackgroundPaint(Color.white);
		return jfreechart;
	}
	
	public JFreeChart createLineChart() {
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
		//timeseriescollection.addSeries(totalPushed);
		//timeseriescollection.addSeries(highPrioMemPush);
		//timeseriescollection.addSeries(memPush);
		//timeseriescollection.addSeries(cachePush);
		//timeseriescollection.addSeries(diskPush);
		timeseriescollection.addSeries(pop);
		
		DateAxis dateaxis = new DateAxis("Time");
		NumberAxis numberaxis = new NumberAxis("Count");
		dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
		numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
		dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
		numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
		numberaxis.setAutoRangeIncludesZero(false);
		
		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(true, false);
		xylineandshaperenderer.setSeriesPaint(0, Color.red);
		/*xylineandshaperenderer.setSeriesPaint(0, Color.red);
		xylineandshaperenderer.setSeriesPaint(1, Color.green);
		xylineandshaperenderer.setSeriesPaint(2, Color.blue);
		xylineandshaperenderer.setSeriesPaint(3, Color.cyan);
		xylineandshaperenderer.setSeriesPaint(4, Color.yellow);
		xylineandshaperenderer.setSeriesPaint(5, Color.orange);*/
		xylineandshaperenderer.setBaseStroke(new BasicStroke(3F, 0, 2));
      
		XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, xylineandshaperenderer);
		xyplot.setBackgroundPaint(Color.lightGray);
		xyplot.setDomainGridlinePaint(Color.white);
		xyplot.setRangeGridlinePaint(Color.white);
		xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
		xyplot.setNoDataMessage("No data available");
		
		dateaxis.setAutoRange(true);
		dateaxis.setLowerMargin(0.0D);
		dateaxis.setUpperMargin(0.0D);
		dateaxis.setTickLabelsVisible(true);
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
      
		JFreeChart jfreechart = new JFreeChart("Poped URL", new Font("SansSerif", 1, 24), xyplot, true);
		jfreechart.setBackgroundPaint(Color.white);
      
		return jfreechart;
	}
	
	public void setPieChartData(String key, double value) {
		pieDataSet.setValue(key, value);
	}
	
	public void addTotalPushObservation(final double d) {
		totalPushed.addOrUpdate(new Millisecond(), d);
	}
	
	public void addHighPrioMemPushObservation(final double d) {
		highPrioMemPush.addOrUpdate(new Millisecond(), d);
	}
	
	public void addMemPushObservation(final double d) {
		memPush.addOrUpdate(new Millisecond(), d);
	}
	
	public void addCachePushObservation(final double d) {
		cachePush.addOrUpdate(new Millisecond(), d);
	}
	
	public void addDiskPushObservation(final double d) {
		diskPush.addOrUpdate(new Millisecond(), d);
	}
	
	public void addPopObservation(final double d) {
		pop.addOrUpdate(new Millisecond(), d);
	}
	
}
